import { _decorator, Component, Label, Node, Vec2 } from 'cc';
import AbstractComponent from '../../../base/AbstractComponent';
import { BallInfoNode } from './BallInfoNode';
import { App } from '../../../App';
import { GameModel2 } from './GameModel2';
import { Ball } from '../Ball';
import GameUtils2 from './GameUtils2';
import { find } from 'cc';
import { HIT_BALL_TIME, TrainLayer } from './GameConstant2';
import { Sprite } from 'cc';
import { SpriteFrame } from 'cc';
import { instantiate } from 'cc';
import { Prefab } from 'cc';
import { Vec3 } from 'cc';
import { GameBaseModel } from '../GameBaseModel';
import { tween } from 'cc';
import { UIOpacity } from 'cc';
import { Role } from '../Role';
import { UITransform } from 'cc';
import { MeshRenderer } from 'cc';
import { EventConst } from '../../../common/Common';
import { PhysicsSystem } from 'cc';
import { showGameActionPoint } from '../../../tools/PopuUp';
import { ACTION_POINT_R } from '../GameManage';
const { ccclass, property } = _decorator;

@ccclass('GameLayer2')
export class GameLayer2 extends AbstractComponent {
    
    curScore = 0

    @property(BallInfoNode)
    mineInfoCtrl : BallInfoNode = null

    @property(BallInfoNode)
    otherInfoCtrl : BallInfoNode = null

    @property(Label)
    label_time : Label = null // 倒计时

    @property(Prefab)
    inRoleAnimNode : Prefab = null
    
    downTime : number = 0

    protected onLoad(): void {
    }
    
    start() {
        find('btn_test', this.node).active = false

        this.schedule(this.updateTime, 1)
        this.label_time.node.active = false
        this.setActionPoint(new Vec2(0,0))

        //更新作用点位置
        App.Event.on(EventConst.ACTION_BALL_POINT,(data)=>{
            this.setActionPoint(data)
        },this)
    }

    // 更新用户信息
    setUserName(myName, otherName) {
        this.mineInfoCtrl.setName(myName)
        this.otherInfoCtrl.setName(otherName)
    }

    // 更新球列表信息
    updateBallInfo(myBallList, otherBallList) {
        this.mineInfoCtrl.setData(myBallList)
        this.otherInfoCtrl.setData(otherBallList)
    }

    // 设置倒计时
    setActionTime(time = HIT_BALL_TIME) {
        this.downTime = time
        this.setTimeLabel(this.downTime)
        this.label_time.node.active = true
    }

    // 
    stopActionTime() {
        this.downTime = -1
        this.label_time.string = ""
    }

    // 停止时间定时器
    stopTimeSchedule() {
        this.unschedule(this.updateTime)
    }

    /**
     * 显示提示动画
     * 0，待定 1，玩家 2，ai
     */
    showWhoAction(isWho) {
        this.mineInfoCtrl.showActionAnim(isWho == 1)
        this.otherInfoCtrl.showActionAnim(isWho == 2)
    }

    updateTime() {
        
        if(this.downTime > 0) {
            this.downTime -= 1

            if(this.downTime == 0) {
                App.Event.emit(EventConst.ACTION_TIME_END)
            }

            this.setTimeLabel(this.downTime)
        }
        
    }

    setTimeLabel(time : number) {
        this.label_time.string = time.toString() + "s"
    }

    onBtnTest() {

        // if(!this.item) {
        //     this.item = instantiate(this.inRoleAnimNode)
            
        // }
        // this.showAnim(this.item)

        // let role = (App.Model.gameView as GameModel2).roleNodeList[0].getComponent(Role)
        // role.playInRoleAnim()

        // GameUtils2.showLayer(TrainLayer.WIN)

        // this.showAllOneClear()

        // this.showCombo(1)

        // (App.Model.gameView as GameModel2).gameLogic.mine.posWhiteBall()

        // let logic = (App.Model.gameView as GameModel2).gameLogic

        // GameUtils2.showLayer(TrainLayer.FAIL)

    }

    onBtnExit() {
        App.Audio.playOneShot("click")
        GameUtils2.showLayer(TrainLayer.EXIT)
    }
    
    protected onDestroy(): void {
        this.stopTimeSchedule()
        App.Event.off(EventConst.ACTION_BALL_POINT, this.setActionPoint, this)
    }

    protected update(dt: number): void {
    }

    /** 
     * 点击作用点
      */
    clickActionPoint() {
        let gameView = App.Model.gameView as GameBaseModel
        showGameActionPoint(gameView.viewNode)
    }

    /** 
     * 设置作用点
      */
    setActionPoint(pos) {
        let ratio = 50 / ACTION_POINT_R
        let point = find("actionBall/redpoint",this.node)
        point.setPosition(pos.x * ratio, pos.y * ratio)

        if (pos.x == 0 && pos.y == 0) {
            App.Model.gameManage.ballActionDial = 0
            App.Model.gameManage.setActionPoint(pos)
            App.Event.emit(EventConst.GAME_HIT_ACTIONVIEW)
        }
    }

}
